<template>
	<view class="content">
		<view class="head">
			<image src="../../static/image/login-bg.png" mode=""></image>
		</view>
		<view class="back" @click="toBack">
			<image src="../../static/image/back.png" mode=""></image>
		</view>
		<scroll-view style="width:100%;height:100%;" scroll-y="true">
			<view style="width:100%;height:720rpx;"></view>
			<view class="login-box">
				<view class="login-content">
					<view class="title">登錄</view>
					<view class="tips">還沒有賬號，立即<text @click="toRegister">註冊</text></view>
					<view class="one-infor" style="border-bottom:1px solid #F0F0F0">
						<input type="tel" v-model="tel" placeholder-style="color:#9E9E9E" placeholder="請輸入手機號">
					</view>
					<view class="one-infor" v-if="!isCodeLogin">
						<input type="password" v-model="password" placeholder-style="color:#9E9E9E" placeholder="請輸入密碼">
					</view>
					<view class="one-infor" v-else >
						<input type="text" v-model="code" placeholder-style="color:#9E9E9E" placeholder="請輸入驗證碼">
						<view class="send-code" v-if="!haveSend" @click="sendCode">發送驗證碼</view>
						<view class="countDown" @click="sendCode" v-if="haveSend">
							重新發送<text v-if="countNum>0">（{{countNum}}s）</text>
						</view>
					</view>
					<view class="login-btn" @click="loginOk">登錄</view>
					<view class="code" @click="isCodeLogin=true" v-if="!isCodeLogin" >短信驗證</view>
					<view class="code" @click="isCodeLogin=false" v-else>密碼驗證</view>
					<view class="agreement" @click="toAgree">
						<view class="isCheck" v-if="isAgree">
							<image src="../../static/image/check.png" mode=""></image>
						</view>
						<view class="noCheck" v-if="!isAgree"></view>
						<view class="agree">
							註冊即同意<text @click.stop="showAgreement('user_register_content')">《用戶註冊協議》</text><text @click.stop="showAgreement('user_privacy_content')">《隱私政策》</text>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {login,codeLogin} from '../../request/api.js'
	export default {
		data() {
			return {
				isAgree: false,
				countNum:0,
				haveSend:false,
				isCodeLogin:false,
				tel:'',
				password:"",
				code:""
			};
		},
		methods: {
			showAgreement(e){
				uni.navigateTo({
					url:'/pages/agreement/agreement?type='+e
				})
			},
			// 登录
			async loginOk(){
				if(!this.isAgree){
					uni.showToast({
						title: '請閱讀並同意用戶協議',
						icon: 'none'
					})
					return
				}
				if(!this.tel){
					uni.showToast({
						title: '請填寫手機號',
						icon: 'none'
					})
					return
				}
				if (!/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(this.tel)){
					uni.showToast({
						title: '請填寫正確手機號',
						icon: 'none'
					})
					return
				}
				if(!this.password&&!this.isCodeLogin){
					uni.showToast({
						title: '請填寫密碼',
						icon: 'none'
					})
					return
				}
				if(!this.code&&this.isCodeLogin){
					uni.showToast({
						title: '請填寫驗證碼',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					title:'登錄中...'
				})
				if(this.isCodeLogin){
					let res=await codeLogin({
						mobile:this.tel,
						captcha:this.code
					})
					uni.hideLoading();
					console.log(res);
					if(res.code==1){
						uni.showToast({
							title: '登錄成功',
							icon: 'none'
						})
						uni.setStorageSync('token', res.data.userinfo.token);
						uni.setStorageSync('myInfor',JSON.stringify(res.data.userinfo));
						this.$store.commit("user/CHANGE_MY_INFOR",res.data.userinfo);
						this.$store.commit('user/CHANGE_LOGIN_STATUS',true);
						uni.redirectTo({
							url:'/pages/index/index'
						})
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}else{
					let res=await login({
						mobile:this.tel,
						password:this.password
					})
					uni.hideLoading();
					console.log(res);
					if(res.code==1){
						uni.showToast({
							title: '登錄成功',
							icon: 'none'
						})
						uni.setStorageSync('token', res.data.userinfo.token);
						uni.setStorageSync('myInfor',JSON.stringify(res.data.userinfo));
						this.$store.commit("user/CHANGE_MY_INFOR",res.data.userinfo)
						this.$store.commit('user/CHANGE_LOGIN_STATUS',true);
						uni.redirectTo({
							url:'/pages/index/index'
						})
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}
			},
			// 同意协议
			toAgree() {
				this.isAgree = !this.isAgree
			},
			toBack() {
				uni.navigateBack()
			},
			// 去注册
			toRegister() {
				uni.navigateTo({
					url: '/pages/register/register'
				})
			},
			// 发送验证码
			sendCode(){
				if(this.countNum>0){
					return;
				}
				this.haveSend=true;
				this.countNum=60;
				let timeInter=setInterval(()=>{
					if(this.countNum>0){
						this.countNum--;
					}else{
						clearInterval(timeInter);
					}
				},1000)
				
			}
		}
	}
</script>

<style lang="scss">
	.content {
		width: 100%;
		height: 100vh;

		.head {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 720rpx;
			z-index: -1;

			image {
				width: 100%;
				height: 720rpx;
			}
		}

		.back {
			width: 55rpx;
			height: 55rpx;
			position: absolute;
			top: 80rpx;
			left: 30rpx;
			z-index: 1000;

			image {
				width: 55rpx;
				height: 55rpx;
			}
		}

		.login-box {
			width: 100%;
			background-color: #fff;
			padding-top: 60rpx;
			border-top-left-radius: 15rpx;
			border-top-right-radius: 15rpx;
			padding-bottom: 50rpx;
		}

		.login-content {
			width: 650rpx;
			margin-left: auto;
			margin-right: auto;

			.title {
				font-size: 40rpx;
				color: #040404;
				line-height: 56rpx;
				font-weight: bold;
			}

			.tips {
				font-size: 24rpx;
				colro: #040404;
				line-height: 33rpx;
				margin-top: 15rpx;
				margin-bottom: 30rpx;

				text {
					color: #D71B0A;
				}
			}
			.one-infor{
				padding:40rpx 0rpx;width:100%;
				display: flex;align-items: center;
				justify-content:space-between;
				padding-right:40rpx;
				input{
					font-size:28rpx;color:#333;flex:1;
				}
				.send-code{
					font-size: 28rpx;color:#333333;
				}
				.countDown{
					font-size: 28rpx;color:#D71B0A;
				}
			}
		}

		.login-btn {
			width: 100%;
			height: 98rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			background-color: #040404;
			color: #fff;
			font-size: 36rpx;
			font-weight: 500;
			border-radius: 98rpx;
			margin-top: 80rpx;
		}

		.code {
			font-size: 28rpx;
			color: #9E9E9E;
			margin-top: 30rpx;
			line-height: 40rpx;
			text-align: center;
		}

		.agreement {
			margin-top: 109rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			.isCheck {
				width: 26rpx;
				height: 26rpx;

				image {
					width: 26rpx;
					height: 26rpx;
					display: block;
				}
			}

			.noCheck {
				width: 26rpx;
				height: 26rpx;
				box-sizing: border-box;
				border: 1px solid #9E9E9E;
				border-radius: 50%;
			}

			.agree {
				font-size: 24rpx;
				color: #9E9E9E;
				margin-left: 10rpx;

				text {
					color: #040404
				}
			}
		}
	}
</style>